package com.holy.experiment11.service;

import com.holy.experiment11.Student.Student;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service //标识该类是一个服务组件，spring会自动扫描和管理
public class StudentService {
    private List<Student> students = new ArrayList<>();

    public StudentService() {
        //初始一些学生数据
        students.add(new Student(1L, "22090202128", "我", "22@example.com", 22));
        students.add(new Student(2L, "23090202128", "我", "23@example.com", 23));
        students.add(new Student(3L, "21090202128", "我", "21@example.com", 21));
        students.add(new Student(4L, "20090202128", "我", "20@example.com", 20));
    }
    public List<Student> findAllStudents() {  //顾名思义，返回所有学生列表
        return students;
    }

    public Student createStudent(Student student) {  //同样顾名思义，create嘛创建一个student对象，并返回
        students.add(student);
        return student;
    }

    public Student updateStudent(Long id,Student student) {//更新
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(id)) {  ///比较是否相等，就是一个遍历
                student.setId(id); // 确保ID不变
                return students.set(i, student);
            }
        }
        return null; // 如果没有找到，返回null
    }

    public void deleteStudent(Long id) {//删除
        students.removeIf(student -> student.getId().equals(id));//遍历并删除满足条件的学生
    }
    //模拟CRUD操作的方法
    }



